package com.mall.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Package: com.delivery.result
 * @author: xiao-chen
 * @Create: 2023/3/22-13:53
 * @Version: v 1.0
 * @Description:返回结果类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {
    /**
     * 编码：1成功，0和其它数字为失败
     */
    private Integer code;
    /**
     * 错误信息
     */

    private String msg;
    /**
     * 动态数据
     */
    private T data;


    public static <T> R<T> success(T object) {
        R<T> r = new R<>();
        r.data = object;
        r.code = 1;
        return r;
    }

    public static <T> R<T> success(String msg) {
        R<T> r = new R<>();
        r.msg = msg;
        r.code = 1;
        return r;
    }

    public static <T> R<T> error(String msg) {
        R<T> r = new R<>();
        r.msg = msg;
        r.code = 0;
        return r;
    }

    public static <T> R<T> error(String msg, T data) {
        R<T> r = new R<>();
        r.msg = msg;
        r.code = 0;
        r.data = data;
        return r;
    }

}